package com.example.service;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.entity.AirspaceApproval;
import com.example.entity.info.AirLaneInfo;
import com.example.entity.info.SpecialInfo;
import com.example.entity.info.TakeoffLandInfo;
import com.example.entity.info.WorkInfo;
import com.example.entity.transAirspaceApproval;
import com.example.mapper.AirspaceApprovalMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class AirspaceApprovalService {
    @Autowired
    private AirspaceApprovalMapper airspaceApprovalMapper;

    public List<AirspaceApproval> findAll(){ return airspaceApprovalMapper.selectList(null); }

    public Optional<AirspaceApproval> findById(Long id){ return Optional.ofNullable(airspaceApprovalMapper.selectById(id)); }

    public AirspaceApproval isave(AirspaceApproval airspaceApproval){
        airspaceApprovalMapper.insert(airspaceApproval);
        return airspaceApproval;
    }

    public Optional<AirspaceApproval> update(AirspaceApproval airspaceApproval){
        int i = airspaceApprovalMapper.updateById(airspaceApproval);
        if(i == 1){
            return Optional.of(airspaceApproval);
        }
        return Optional.empty();
    }

    public Optional<AirspaceApproval> update2(Integer id, AirspaceApproval airspaceApproval){
        AirspaceApproval airspaceApproval1 = airspaceApprovalMapper.selectById(id);
        airspaceApproval1.setDocumentsReceiving(airspaceApproval.getDocumentsReceiving());
        airspaceApproval1.setDocumentsPreview(airspaceApproval.getDocumentsPreview());
        QueryWrapper<AirspaceApproval> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int i = airspaceApprovalMapper.update(airspaceApproval1, queryWrapper);
        if(i == 1){
            return Optional.of(airspaceApproval1);
        }
        return Optional.empty();
    }

    public boolean delete(Long id){
        if(airspaceApprovalMapper.selectById(id) != null){
            airspaceApprovalMapper.deleteById(id);
            return true;
        }
        return false;
    }

    public List<AirspaceApproval> searchsorted(String keyword, String sortField, Boolean sortOrder){
        QueryWrapper<AirspaceApproval> wrapper = new QueryWrapper<>();
        if(keyword != null && !keyword.isEmpty()){
            wrapper.like("user_name",keyword).or()
            .like("application_acceptance_time",keyword).or()
            .like("id",keyword);
        }
        //设置排序规则
        if(sortOrder){
            wrapper.orderByAsc(sortField); // 降序
        }
        else{
            wrapper.orderByDesc(sortField);  // 升序
        }
        List<AirspaceApproval> sysGenList = airspaceApprovalMapper.selectList(wrapper);
        return sysGenList;

    }


    //关键字搜索搜索搜索搜索
    public List<transAirspaceApproval> searchandsort(String keyword, String sortField, Boolean sortOrder) {
        QueryWrapper<AirspaceApproval> wrapper = new QueryWrapper<>();
        if (keyword != null && !keyword.isEmpty()) {
            // 模糊匹配 program_name、application_acceptance_time 和 id 字段
            wrapper.like("approval_file_name", keyword)
                    .or()
                    .like("application_acceptance_time", keyword) // 假设 application_acceptance_time 被格式化为字符串
                    .or()
                    .like("id", keyword);
        }
        //设置排序规则
        if(sortOrder){
            wrapper.orderByAsc(sortField); // 降序
        }
        else{
            wrapper.orderByDesc(sortField);  // 升序
        }

        List<AirspaceApproval> airspaceApproval1s = airspaceApprovalMapper.selectList(wrapper);
        return airspaceApproval1s.stream().map(airspaceApproval1 -> {
            transAirspaceApproval transAirspaceApproval = new transAirspaceApproval();
            BeanUtils.copyProperties(airspaceApproval1, transAirspaceApproval);
            String takeoffland = airspaceApproval1.getTakeoffLandInfo();
            String airlane = airspaceApproval1.getAirLaneInfo();
            String work = airspaceApproval1.getWorkInfo();
            String special = airspaceApproval1.getSpecialInfo();
            if(takeoffland != null && !takeoffland.isEmpty()) {
                JSONArray jsonArray = JSONArray.parseArray(takeoffland);
                List<TakeoffLandInfo> takeoffLandInfos = jsonArray.toJavaList(TakeoffLandInfo.class);
                transAirspaceApproval.setTakeoffLandInfo(takeoffLandInfos);
            }
            if(airlane != null && !airlane.isEmpty()) {
                JSONArray jsonArray = JSONArray.parseArray(airlane);
                List<AirLaneInfo> airLaneInfos = jsonArray.toJavaList(AirLaneInfo.class);
                transAirspaceApproval.setAirLaneInfo(airLaneInfos);
            }
            if(work != null && !work.isEmpty()) {
                JSONArray jsonArray = JSONArray.parseArray(work);
                List<WorkInfo> workInfos = jsonArray.toJavaList(WorkInfo.class);
                transAirspaceApproval.setWorkInfo(workInfos);
            }
            if(special != null && !special.isEmpty()) {
                JSONArray jsonArray = JSONArray.parseArray(special);
                List<SpecialInfo> speciaInfos = jsonArray.toJavaList(SpecialInfo.class);
                transAirspaceApproval.setSpecialInfo(speciaInfos);
            }
            return transAirspaceApproval;
        }).collect(Collectors.toList());
    }
}
